package edu.stu.mall.fragment.knowledge

import android.app.Application
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import edu.mall.base.BaseViewModel
import edu.mall.base.SingleLiveEvent
import edu.stu.mall.repository.Repository
import edu.stu.mall.repository.data.BannerListData
import edu.stu.mall.repository.data.CartListData
import edu.stu.mall.repository.data.CartListDataItem
import edu.stu.mall.repository.data.RecommendedProductData
import edu.stu.mall.repository.data.RecommendedProductDataItem
import kotlinx.coroutines.launch

class FragKnowledgeViewModel(application: Application) : BaseViewModel(application) {

    var cartListDataItem = SingleLiveEvent<List<CartListDataItem>?>()
    val isEmptyCart = MutableLiveData<Boolean>()

    init {

        getCartList()

    }

    private fun getCartList() {
        viewModelScope.launch {
            val data: CartListData? = Repository.getCartList()

            if (data?.isNotEmpty() == true) {
                data.forEach {
                    Log.e("TAG", "getCartList: ${it.productName}")
                }
                cartListDataItem.postValue(data)
                isEmptyCart.postValue(false) // 购物车不为空
            } else {
                ToastUtils.showShort("购物车无内容")
                isEmptyCart.postValue(true) // 购物车为空

            }
        }
    }
}